unit Struct;

interface

uses SysUtils, DB, DBTables, Classes;

const
  MAX_FIELDS = 100;

type
  TTableStruct = class(TComponent)
    Table: TTable;
  private
    FTemplate: string;
    procedure SetTemplate(Value: string);
  public
    FSizes: array[1..MAX_FIELDS] of integer;
    constructor Create(AOwner: TComponent); override;
    property Template: string read FTemplate write SetTemplate;
    procedure Load(FileName: string);
    procedure Unload(FileName: string; Overwrite: boolean);
    procedure Reply(FileName: string; Overwrite, Accept: boolean);
    procedure AcceptDoc;
  end;

implementation

uses WinDos, HyperStr, Bank;

var
  F: TextFile;
  i, k: integer;
  S, S1: string;

constructor TTableStruct.Create(AOwner:TComponent);
begin
  inherited;
  Table := TTable.Create(Self);
end;

procedure TTableStruct.SetTemplate(Value: string);
begin
  AssignFile(F, Value);
  FileMode := fmOpenRead;
  Reset(F);
  i := 0;
  k := 0;
  With Table do begin
    Active := false;
    DatabaseName := 'TempData';
    TableType := ttDBase;
    TableName := 'postdata.dbf';
    FieldDefs.Clear;
  end;

  while not eof(F) do
  begin
    readln(F, S);
    i := i + 1;
    if (trim(S) <> '') and (Copy(S, 1, 17) <> 'Number of fields:') then begin
      k := k + 1;
      with Table.FieldDefs.AddFieldDef do begin
        Name := Trim(Copy(S, 1, 12));
        S1 := Copy(S, 25, 6);
        FSizes[k] := StrToInt(Trim(Copy(S, 25, 6)));
        case S[13] of
          'N': if Trim(Copy(S, 32, 3)) = '' then
                 DataType := ftInteger
               else if StrToInt(Trim(Copy(S, 32, 3))) <= 2 then DataType := ftCurrency
                    else DataType := ftFloat;
          'D': DataType := ftDate;
          'C': begin
                 DataType := ftString;
                 Size := FSizes[k];
               end;
          'L': DataType := ftBoolean;
        end;
        Required := False;
      end;

    end;
  end;
  CloseFile(F);
  try
    Table.CreateTable;
    Table.Open;
  except
    Table.Free;
  end;
end;

procedure TTableStruct.Load(FileName: string);
var
  i, y, m, d: integer;
  iPos, IntPart, FracPart: integer;
begin
  AssignFile(F, FileName);
  FileMode := fmOpenReadWrite;
  Reset(F);
  while not Eof(F) do begin
    Readln(F, S);
    Table.Append;
    iPos := 1;
    for i := 0 to Table.FieldCount - 1 do begin
      if Table.FieldDefs[i].DataType = ftBoolean then begin
        S1 := Trim(Copy(S, iPos, FSizes[i+1]));
        if S1 = 'T' then
          Table.Fields[i].AsBoolean := True
        else
          Table.Fields[i].AsBoolean := False;
      end;
      if Table.FieldDefs[i].DataType = ftInteger then begin
        S1 := Trim(Copy(S, iPos, FSizes[i+1]));
        if S1 <> '' then Table.Fields[i].AsInteger := StrToInt(S1);
      end;
      if Table.FieldDefs[i].DataType = ftFloat then begin
        S1 := Trim(Copy(S, iPos, FSizes[i+1]));
        if S1 <> '' then begin
          IntPart := StrToInt(Copy(S1, 1, Length(S1)-3));
          FracPart := StrToInt(Copy(S1, Length(S1)-1, 2));
          Table.Fields[i].Value := IntPart + FracPart/100;
        end;
      end;
      if Table.FieldDefs[i].DataType = ftDate then begin
        S1 := Trim(Copy(S, iPos, FSizes[i+1]));
        if S1 <> '' then begin
          y := StrToInt(Copy(S1, 1, 4));
          m := StrToInt(Copy(S1, 5, 2));
          d := StrToInt(Copy(S1, 7, 2));
          Table.Fields[i].AsDateTime := EncodeDate(y, m, d);
        end;
      end;
      if Table.FieldDefs[i].DataType = ftString then begin
        S1 := Trim(Copy(S, iPos, FSizes[i+1]));
        Table.Fields[i].AsString := WinString(S1);
      end;
      iPos := iPos + FSizes[i+1];
    end;
    Table.Post;
  end;
  CloseFile(F);
end;

procedure TTableStruct.Unload(FileName: string; Overwrite: boolean);
var i: integer;
begin
  AssignFile(F, FileName);
  if Overwrite or (not FileExists(FileName)) then
    Rewrite(F)
  else Append(F);
  Table.First;
  While not Table.Eof do begin
    S := '';
    for i := 0 to Table.FieldCount - 1 do
      case Table.FieldDefs[i].DataType of
        ftBoolean: begin
                     if Table.Fields[i].AsBoolean then S1 := 'T' else S1 := 'F';
                     S := S + S1;
                   end;
        ftInteger: begin
                     S1 := Table.Fields[i].AsString;
                     LPad(S1, ' ', FSizes[i+1]);
                     S := S + S1;
                   end;
        ftFloat: begin
                   S1 := FormatCurr('0.00', Table.Fields[i].AsCurrency);
                   LPad(S1, ' ', FSizes[i+1]);
                   S := S + S1;
                 end;
        ftString: begin
                    S1 := Table.Fields[i].AsString;
                    RPad(S1, ' ', FSizes[i+1]);
                    S := S + DosString(S1);
                  end;
        ftDate: if Table.Fields[i].IsNull then S := S + '        '
                else begin
                  S1 := Table.Fields[i].AsString;
                  S1 := StrictDate(S1);
                  S := S + S1;
                end;
      end;
    writeln(F, S);
    Table.Next;
  end;
  CloseFile(F);
end;

procedure TTableStruct.Reply(FileName: string; Overwrite, Accept: boolean);
var sSum: string;
begin
  AssignFile(F, FileName);
  if Overwrite or (not FileExists(FileName)) then
    Rewrite(F)
  else Append(F);
  S := Table.FieldValues['LSPL'] + '     ';
  S := S + StrictDate(Table.FieldByName('DATED').AsString);
  S := S + LAdd(Table.FieldByName('PL_IDX').AsString, ' ', 9);
  S := S + LAdd(Table.FieldByName('ND').AsString, ' ', 6);
  sSum := FormatCurr('0.00', Table.FieldByName('SUM').AsCurrency);
  S := S + LAdd(sSum, ' ', 17);
  S := S + LAdd(Table.FieldByName('BICPOL').AsString, ' ', 9);
  S := S + LAdd(Table.FieldByName('NEW_ST').AsString, ' ', 2);
  S := S + LAdd(Table.FieldByName('ST').AsString, ' ', 2);
  writeln(F, S);
  CloseFile(F);
end;

procedure TTableStruct.AcceptDoc;
begin

end;

end.
